/**
 * @action对象生成文件
 * 
 * 一个页面可以抛出多个export
 * 但是只可以抛出一个export default
 */
import axios from 'axios'
import '@/mock/index'

export const getInputChange = (value) => {
  return {
    type: 'change_input_value',
    value
  }
}

export const getBtnClick = () => {
  return {
    type: 'add_btn_click'
  }
}

export const getDeleteItem = (index) => {
  return {
    type: 'delete_list_item',
    index
  }
}

export const getInitData = (data) => {
  return {
    type: 'init_todolist_data',
    data
  }
}

// initGetData最终也是生成了一个action提交对象。只不过在他的内部可以使用函数
export const initGetData = () => {
  return (dispatch) => {
    axios.get('http://localhost:3002/api/get-todolist').then(res => {
      const data = res.data.list
      const action = getInitData(data)
      dispatch(action)
    })
  }
}

export const getCartData = (data) => {
  return {
    type: 'get_cart_data',
    data
  }
}

export const initCartData = () => {
  return (dispatch) => {
    axios.get('/api/cart').then(res => {
      dispatch(getCartData(res.data))
    }) 
  }
}

export const getToCart = (index) => {
  return {
    type: 'to_cart',
    index
  }
}